﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace MiniFox.Infrastructure
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable, JsonObject]
    public sealed class ViewRow
    {
        ViewColumnCollection _viewColumns;
        IDictionary<string, object> _rowValues;

        internal ViewRow(ViewColumnCollection viewColumns)
        {
            _viewColumns = viewColumns;
            _rowValues = new Dictionary<string, object>();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public object GetValue(string columnName)
        {
            return this[columnName];
        }
        /// <summary>
        /// 行数据
        /// </summary>
        [JsonProperty("dataItem")]
        public object DataItem
        {
            get
            {
                return _rowValues;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        [JsonIgnore]
        public object this[string columnName]
        {
            get
            {
                if (!_viewColumns.Contains(columnName))
                {
                    throw new IndexOutOfRangeException();
                }
                if (!_rowValues.Keys.Contains(columnName))
                {
                    _rowValues.Add(columnName, null);
                }
                return _rowValues[columnName];
            }
            set
            {
                if (!_viewColumns.Contains(columnName))
                {
                    throw new IndexOutOfRangeException();
                }
                if (!_rowValues.Keys.Contains(columnName))
                {
                    _rowValues.Add(columnName, value);
                }
                else
                {
                    _rowValues[columnName] = value;
                }
            }
        }
    }
    /// <summary>
    /// 行状态枚举
    /// </summary>
    public enum RowViewState : short
    {
        /// <summary>
        /// 标记为未更改
        /// </summary>
        Unchanged = 0,
        /// <summary>
        /// 标记为已更改
        /// </summary>
        Changed = 1,
        /// <summary>
        /// 标记为已删除
        /// </summary>
        Removed = -1
    }
}
